package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.User;
import com.itheima.service.UserService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserController {

    @Reference
    private UserService userService;

    @Autowired
    private BCryptPasswordEncoder passwordEncoder;

    // 获取登录的用户名
    @RequestMapping("/user/getUsername")
    public Result getUsername() {
        org.springframework.security.core.userdetails.User user = (org.springframework.security.core.userdetails.User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if (user != null) {
            String username = user.getUsername();
            if (username != null) {
                return new Result(true, MessageConstant.GET_USERNAME_SUCCESS, username);
            }
        }
        return new Result(false, MessageConstant.GET_USERNAME_FAIL);
    }

    // 动态获取 登录者可以看到的 菜单列表
    @RequestMapping("/user/getPermissions")
    public Result getPermissions(String username){
        return userService.getPermissions(username);
    }


    //  /checkitem/findPage 分页查询 用户信息
    @RequestMapping("/user/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        return userService.findPage(queryPageBean);
    }

    // /user/add  新增用户
    @RequestMapping("/user/add")
    public Result add(Integer[] checkitemIds, @RequestBody User user) {
        String password = user.getPassword();
        user.setPassword(passwordEncoder.encode(password));
        return userService.insert(user, checkitemIds);
    }

    // /user/getAllRole 查询所有的角色信息

    @RequestMapping("/user/getAllRole")
    public Result getAllRole() {
        return userService.getAllRole();
    }

    // 编辑显示  /user/findById

    @RequestMapping("/user/findById")
    public Result findById(Integer id){
        return userService.findById(id);
    }

    // /checkgroup/findCheckItemIdsByCheckGroupId
    @RequestMapping("/user/findUserIdsByRoleId")
    public Result findCheckItemIdsByCheckGroupId(Integer id){
        return userService.findRoleIdsByUserId(id);
    }

    // /user/edit 编辑
    @RequestMapping("/user/edit")
    public Result edit(Integer[] checkitemIds, @RequestBody User user) {
        return userService.edit(checkitemIds,user);
    }

    // /user/delete 删除用户
    @RequestMapping("/user/delete")
    public Result delete(Integer id){
        return userService.delete(id);
    }



}
